﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using System.Collections.Generic;
using System.Linq;

namespace UtilLib
{
    /// <summary>
    /// 公共函数类  Dictionary 嵌套 List
    /// </summary>
    public class DictList<TKey, TValue> : Dictionary<TKey, List<TValue>>
    {
        /// <summary>
        /// 
        /// </summary>
        public DictList() 
            : base()
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="values"></param>
        /// <param name="newList"></param>
        /// <returns></returns>
        public bool TryGetValues(TKey key, out List<TValue> values, bool newList = true)
        {
            bool result = TryGetValue(key, out values);
            if (newList && !result)
            {
                values = new List<TValue>();
            }
            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Add(TKey key, TValue value)
        {
            if (!TryGetValues(key, out var values))
            {
                base.Add(key, values);
            }
            values.Add(value);
        }
        /// <summary>
        /// 添加list 不重复
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void TryAdd(TKey key, TValue value)
        {
            if (!TryGetValues(key, out var values))
            {
                base.Add(key, values);
            }
            values.TryAdd(value);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool ContainsValue(TKey key, TValue value)
        {
            if (TryGetValue(key, out var values))
            {
                return values.Contains(value);
            }
            return false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool Remove(TKey key, TValue value)
        {
            if (TryGetValue(key, out var values))
            {
                return values.Remove(value);
            }
            return false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="other"></param>
        public void Merge(DictList<TKey, TValue> other)
        {
            if (other == null)
            {
                return;
            }
            foreach (var kvp in other)
            {
                foreach (var value in kvp.Value)
                {
                    this.Add(kvp.Key, value);
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="index"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool TryGetIndexValue(TKey key, int index, out TValue value)
        {
            try
            {
                value = base[key][index];
                return true;
            }
            catch
            {
                value = default(TValue);
                return false;
            }
        }

    }
}